package com.dfdchem.imd.kaiwu.fragment;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.dfdchem.imd.kaiwu.R;
import com.dfdchem.imd.kaiwu.activity.base.BaseActivityUM;
import com.dfdchem.imd.kaiwu.manager.GlideManager;
import com.dfdchem.imd.kaiwu.util.StringUtil;

import java.util.Map;
import java.util.Set;

import zuo.biao.library.util.ScreenUtil;

public class FullImageDialog extends DialogFragment implements View.OnClickListener, RequestListener<Drawable> {

    private static final String TAG = "FullImageDialog";

    public static final int DialogStyle = 3; // 全屏图片，下面一个关闭按钮，适用于海报活动，参数为 imageUrl, uri

    private BaseActivityUM baseActivityUM;
    private Map<String, String> data;

    private View close;
    private ImageView imageView;

    private String imageUrl;
    private String url;

    private FragmentManager manager;
    private String tag;

    public FullImageDialog(BaseActivityUM baseActivityUM, Map<String, String> data) {
        this.baseActivityUM = baseActivityUM;
        this.data = data;

        if (data != null) {
            Set<String> keys = data.keySet();

            if (keys.contains("imageUrl")) {
                this.imageUrl = data.get("imageUrl");
            }
            if (keys.contains("url")) {
                this.url = data.get("url");
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.dialog_fragment_full_screen_image, container);
        initView(view);
        initData();
        initEvent();
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    private void resizeWindowLayout(int imgWidth, int imgHeight){
        Dialog dialog = getDialog();
        if (dialog != null) {
            Window window = dialog.getWindow();
            DisplayMetrics dm = getActivity().getResources().getDisplayMetrics();
            WindowManager.LayoutParams lp = getDialog().getWindow().getAttributes();
            lp.width = (int) (8 * ScreenUtil.getScreenWidth(getContext()) / 10);
            lp.height = (int) (lp.width * imgHeight / imgWidth + 56 * dm.density);
            if (window != null) {
                window.setLayout(lp.width, lp.height);
                window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            }
        }
    }

    public void initView(View view) {
        close = view.findViewById(R.id.close);
        imageView = view.findViewById(R.id.imageView);
    }

    public void initData() {
        if (imageUrl != null) {
            new GlideManager(getActivity()).load(imageView,
                    imageUrl,
                    RequestOptions.bitmapTransform(new RoundedCorners(30)),
                    this
            );
        }
    }

    public void initEvent() {
        close.setOnClickListener(this);
        imageView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close:
                dismiss();
                break;
            case R.id.imageView:
                if (!StringUtil.isEmpty(url)) {
                    baseActivityUM.handleScheme(Uri.parse(url));
                }
                break;
        }
    }

    @Override
    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
        return false;
    }

    @Override
    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
        resizeWindowLayout(resource.getIntrinsicWidth(), resource.getIntrinsicHeight());
        return false;
    }
}
